package com.csap.framework.integration.producer;

import com.aliyun.openservices.ons.api.Message;
import com.csap.framework.util.Assert;
import com.csap.framework.util.FinalConfigParam;
import com.csap.framework.util.StringUtil;
import com.csap.framework.util.json.JsonUtilFastjson;

/**
 * @author yangchengfu
 * @description 发送消息对象
 * @dataTime 2020年-10月-12日 16:12:00
 **/
public class RocketmqMessage extends Message {


    public static RocketmqMessage build() {
        return new RocketmqMessage();
    }

    /**
     * 赋值订阅
     *
     * @param topic 订阅名称
     * @return 返回当前对象
     */
    public RocketmqMessage topic(String topic) {
        Assert.notEmpty(topic, "topic not null");
        this.setTopic(topic);
        return this;
    }

    /**
     * 赋值消息键
     *
     * @param key 键
     * @return 返回当前对象
     */
    public RocketmqMessage key(String key) {
        if (StringUtil.isNotEmpty(key)) {
            this.setKey(key);
        }
        return this;
    }

    /**
     * 赋值消息体
     *
     * @param body 消息内容
     * @return 返回当前对象
     */
    public RocketmqMessage body(byte[] body) {
        this.setBody(body);
        return this;
    }

    public RocketmqMessage body(String body) {
        this.setBody(body.getBytes(FinalConfigParam.DEFAULT_CHARSET));
        return this;
    }

    public RocketmqMessage body(Object body) {
        return body(JsonUtilFastjson.toJSONString(body));
    }

    public RocketmqMessage tag(String tag) {
        if (StringUtil.isNotEmpty(tag)) {
            this.setTag(tag);
        }
        return this;
    }

    public RocketmqMessage msgId(String msgId) {
        if (StringUtil.isNotEmpty(msgId)) {
            this.setMsgID(msgId);
        }
        return this;
    }
}
